//
//  XGVisitorView.swift
//  WeIBo
//
//  Created by 苏文潇 on 2017/3/5.
//  Copyright © 2017年 苏文潇. All rights reserved.
//

import UIKit

class XGVisitorView: UIView {
    
//    MARK:懒加载控件
//   MARK: 旋转视图
    private lazy var cycleImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
//    MARK:罩层
    private lazy var maskImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
//    MARK: 主页视图
    private lazy var homeImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
//    MARK: 底部label
    private lazy var textLabel: UILabel = {
    
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        
        label.textAlignment = .center
        label.numberOfLines = 0
        
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
    
        return label
    }()
    
//    MARK: 注册按钮
    private lazy var registBtn: UIButton = {
    
        let button = UIButton()
        
        button.setTitle("注册", for: .normal)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
//        取消点击高亮的效果
        button.adjustsImageWhenHighlighted = false
        
        button.addTarget(self, action: #selector(registButtonCleck), for: .touchUpInside)
        return button
    }()
    
//    MARK: 登录按钮
    private lazy var loadBtn: UIButton = {
        
        let button = UIButton()
        
        button.setTitle("登录", for: .normal)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
//        取消点击高亮的效果
        button.adjustsImageWhenHighlighted = false
//        点击事件
        button.addTarget(self, action: #selector(loadButtonCleck), for: .touchUpInside)
        return button
    }()
    
//    MARK:  闭包，回调点击登录和注册按钮到控制器
    var callBack:(()->())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK:
    private func setUI()
    {
        self.backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        addSubview(cycleImage)
        addSubview(maskImage)
        addSubview(homeImage)
        addSubview(textLabel)
        addSubview(registBtn)
        addSubview(loadBtn)
        
        cycleImage.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        maskImage.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImage)
        }
        
        homeImage.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImage)
        }
        
        textLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(cycleImage.snp_bottom)
            make.width.equalTo(224)
        }
        
        registBtn.snp_makeConstraints { (make) in
            make.left.equalTo(textLabel)
            make.top.equalTo(textLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        
        loadBtn.snp_makeConstraints { (make) in
            make.right.equalTo(textLabel)
            make.top.equalTo(textLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
//       开始旋转动画
        startAnimation()
    }
  
    
//    MARK: 设置旋转动画
    private func startAnimation(){
//        创建核心动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
//        设置时长
        animation.duration = 20
//         目的地
        animation.toValue = 2 * M_PI
//        重复次数
        animation.repeatCount = MAXFLOAT
//        失去焦点不让其变动
        animation.isRemovedOnCompletion = false
//        添加到layer上
        cycleImage.layer.add(animation, forKey: nil)
        
    }

    
//    MARK: 注册按钮的点击事件
    @objc private func registButtonCleck(){
        
        print("访客视图点击注册,点击已经传入visitor控制器中")
        
        callBack?()
        
    }
    
    
//    MARK:登录按钮的点击事件
    @objc private func loadButtonCleck()
    {
    
        print("访客视图点击登录，点击已经传入visitor控制器中")
        callBack?()
    
    }
    
//    MARK:  切换视图
    func exchangeVisitorView(imageName: String?, title: String?)
    {
        if let imageName = imageName, let title = title {
//            首先隐藏旋转的视图
            cycleImage.isHidden = true
//            设置图片
            self.homeImage.image = UIImage(named: imageName)
//            设置字体
            self.textLabel.text = title
            
        }else{
//        直接开启旋转视图即可，不需要在设置isHidden，因为根本就不是同一个控制器
            startAnimation()
        }
    }
    
    
}






